//
//  MDDOrderListCell.m
//  com.modada.new
//
//  Created by ferver7 on 2019/5/26.
//  Copyright © 2019 ferver7. All rights reserved.
//

#import "MDDOrderListCell.h"
#import "OrderItemGoodCell.h"
#import "OrderItemHeaderCell.h"

@implementation MDDOrderListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selfTableView.delegate = self;
    self.selfTableView.dataSource = self;
    [self.typeBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)btnAction:(UIButton *)sender
{
    if (self.orderModel.orderStatus.integerValue == 0) {
        if (self.cancerOrderBlock) {
            self.cancerOrderBlock();
        }
    }else if (self.orderModel.orderStatus.integerValue == 10){
        if (self.sureOrderBlock) {
            self.sureOrderBlock();
        }
    }else if(self.orderModel.orderStatus.integerValue == 20  ){
        if (self.delOrderBlock) {
            self.delOrderBlock();
        }
    }
}
-(void)reloadViewForModel:(MDDOrderModel *)orderModel
{
    _orderModel = orderModel;
    self.orderNumsLabel.text = [NSString stringWithFormat:@"单号:%@",orderModel.orderNo];
    self.orderInfoLabel.text = [NSString stringWithFormat:@"共%ld种商品  总金额:%@",orderModel.goods.count,orderModel.orderPrice];
    if (orderModel.orderType.integerValue == 1) {
        self.typeLabel.text = @"商家";
    }else{
        self.typeLabel.text = @"专营";
    }
    if (orderModel.orderStatus.integerValue == 0) {
        self.stateLabel.text = @"待发货";
        [self.typeBtn setTitle:@"取消订单" forState:UIControlStateNormal];
    }else if (orderModel.orderStatus.integerValue == 10){
        self.stateLabel.text = @"已发货";
        [self.typeBtn setTitle:@"确认收货" forState:UIControlStateNormal];
    }else if(orderModel.orderStatus.integerValue == 20  ){
        self.stateLabel.text = @"已送达";
         [self.typeBtn setTitle:@"删除订单" forState:UIControlStateNormal];
    }
    [self.selfTableView reloadData];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.orderModel.goods.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary * info = self.orderModel.goods[section];
    return [info[@"attList"] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary * goodInfo = self.orderModel.goods[indexPath.section];
    NSDictionary * itemIndo = goodInfo[@"attList"][indexPath.row];
    OrderItemGoodCell * itemCell = [[NSBundle mainBundle]loadNibNamed:@"OrderItemGoodCell" owner:nil options:nil].lastObject;
    itemCell.xinHaoLabel.text = itemIndo[@"attName"];
    itemCell.priceLabel.text = [NSString stringWithFormat:@"单价:%@/%@",itemIndo[@"price"],goodInfo[@"unit"]];
    itemCell.numsLabel.text = [NSString stringWithFormat:@"x%@",itemIndo[@"quantity"]];
    return itemCell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSDictionary * goodInfo = self.orderModel.goods[section];
    OrderItemHeaderCell * header = [[NSBundle mainBundle]loadNibNamed:@"OrderItemHeaderCell" owner:nil options:nil].lastObject;
    [header.logoImage sd_setImageWithURL:goodInfo[@"thumbnail"] placeholderImage:nil];
    header.storeNameLabel.text = goodInfo[@"name"];
    return header;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
+(CGFloat)cellHeigtForModel:(MDDOrderModel *)orderModel
{
    NSInteger countGood = orderModel.goods.count;
    NSInteger goodNum = 0;
    for (NSDictionary * goodInfo in orderModel.goods) {
        goodNum += [goodInfo[@"attList"] count];
    }
    return countGood*60+90+goodNum*80;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
